package com.five.monkey.feign.consumer.ws;

import com.alibaba.fastjson.JSON;
import com.five.monkey.enums.FunctionTypeEnum;
import com.five.monkey.feign.consumer.client.UserFeignClient;
import com.five.monkey.vo.CommonVo;
import com.five.monkey.vo.UserVo;
import com.five.monkey.ws.UserMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author jim
 * @desc
 * @date 2021-08-15 13:49
 */
@Service
public class UserServer {

    @Autowired
    private UserFeignClient userFeignClient;

    private static Logger logger = LoggerFactory.getLogger(UserServer.class);

    public CommonVo<UserVo> userOerate(String message) {
        CommonVo<UserVo> result;
        logger.info("userOerate消息体,{}", message);
        UserMessage userMessage = JSON.parseObject(message, UserMessage.class);
        FunctionTypeEnum functionType = FunctionTypeEnum.match(userMessage.getFunctionType());
        UserVo userVo = userMessage.getUserVo();
        switch (functionType) {
            case COPY: {
                result = userCopy(userVo.getId());
                break;
            }
            case CREATE: {
                result = userCreate(userVo);
                break;
            }
            case QUERY: {
                result = userQuery(userVo.getId());
                break;
            }
            case UPDATE: {
                result = userUpdate(userVo);
                break;
            }
            case DELETE: {
                result = userDelete(userVo.getId());
                break;
            }
            default: {
                result = CommonVo.exception(1, "functionType=" + userMessage.getFunctionType() + " is error");
            }
        }
        return result;
    }

    private CommonVo<UserVo> userCopy(Long oldUserId) {
        // todo 用户信息拷贝业务逻辑
        return CommonVo.ok(null);
    }

    private CommonVo<UserVo> userCreate(UserVo userVo) {
        // todo 用户创建业务逻辑
        return CommonVo.ok(userVo);
    }

    private CommonVo<UserVo> userQuery(Long id) {
        // todo 用户查询业务逻辑
        return userFeignClient.findById(id);
    }

    private CommonVo<UserVo> userUpdate(UserVo userVo) {
        // todo 用户修改业务逻辑
        return CommonVo.ok(userVo);
    }

    private CommonVo<UserVo> userDelete(Long id) {
        // todo 用户删除业务逻辑
        return CommonVo.ok(null);
    }
}
